/*
 * Vlabs Biological Web Elemments
 *
 * Copyright (c) 2009 Victor de la Torre (vdelatorre@cnio.es)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * http://code.google.com/p/bwe/ 
 *
 */
  (function($) {
 
    $.bwe.fragkb = $.bwe.fragkb || {}; //Add the 'fragkb' scope
    
    
	$.bwe.fragkb.instance = function(container,label,onclick,functionLabel,open){
		
		$.bwe.buidElement(container,label,'$.bwe.fragkb.'+onclick,functionLabel,open);
	}
	
	
	$.bwe.fragkb.getClusterInfo = function(id,label){

 		if ($.bwe.animate(id,800,400)){
	 		
/* 	 		$("#"+id).css('background-color','#F2F2F2'); */
	 		var serv 			= new Fragkb();
	 		var table			= jQuery('<table border="0" style="margin-top:10px;margin-left:20px;"></table>');
	 		var resume			= jQuery('<div class="resume ui-corner-all"></div>');
	 		var tr				= jQuery('<tr></tr>');
	 		var clustertd		= jQuery('<td></td>');
	 		
	 		var fragmentstd1	= jQuery('<td valign="top"></td>');
	 		var fragmentstd2	= jQuery('<td valign="top"></td>');
	 		var fragmentstd3	= jQuery('<td valign="top" style="padding-left: 20px;"></td>');
	 		var fragmentstd4	= jQuery('<td valign="top"></td>');
	 		
	 		fragmentstd1.append(jQuery('<div class="fragment-s">&nbsp;</div>'));
	 		fragmentstd4.append(jQuery('<div class="fragment-s">&nbsp;</div>'));
	 		
	 		var overtd		= jQuery('<td></td>');

			serv.getClusterInfo(function(data){
				
				//cluster label
				clustertd.append('<div class="kbh2 ui-corner-all">Cluster '+label+'</div>');
				
				//fragments
				f = 0;
			
				$.each(data.ClusterInfo.FragmentList.Fragment,function(i,item) {
					
					if (item.attributes.uniprotId){
						var fragmentTable 	= jQuery('<table class="fragment ui-corner-all"></table>');
						var trSequence		= jQuery('<tr></tr>');
						var trKeywords		= jQuery('<tr></tr>');
						
						//keywords
						var keywordstd		= jQuery('<td></td>');
						$.each(item.TagList.Tag,function(i,tag) {
							var keyword = jQuery('<div class="cluster tag" style="width:100px;">'+tag+'</div>');
							keywordstd.append(keyword);
						});
						
						
						trSequence.append(jQuery('<td class="kbh3">'+item.attributes.uniprotId+' ['+item.attributes.sequence+']</td>')); 
						trKeywords.append(keywordstd);
						fragmentTable.append(trSequence);
						fragmentTable.append(trKeywords);
						
						if (f == 0)
							fragmentstd2.append(fragmentTable);
						else if (f == 1)
							fragmentstd3.append(fragmentTable);
						else if (f == 2)
							fragmentstd1.append(fragmentTable);
						else if (f == 3){
							fragmentstd4.append(fragmentTable);
							f = -1;
						}
		
						f++;
					}
				});
/*
				var graph = jQuery('<div id="graph_'+id+'" class="canvas" style="position:relative;width:500px;height:500px;"></div>');
				graph.fillEllipse(10, 150, 100, 100, {color: '#ffd1ff'});
*/
				$("#loading"+id).remove();
				tr.append(fragmentstd1);
				tr.append(fragmentstd2);
				tr.append(clustertd);
				tr.append(fragmentstd3);
				tr.append(fragmentstd4);
				
				table.append(tr);
				$("#"+id).append(table); 
				
				$.each(data.ClusterInfo.OverRepresentedTags.Tag,function(i,tag) {
					resume.append('<span class="resume-tag">'+tag+'</span>');
				});
				$("#"+id).append('<div class="kbh1">Over Represented Tags</div>'); 
				$("#"+id).append(resume); 
			
			},label);
		}
 	}
 	
 	
 })(jQuery);	